#!/usr/bin/perl

use strict;
use warnings;

# tell Perl where to find MozComics package
BEGIN { push @INC, "/home/mozcomic/scrapers/"; }

use MozComics::JSON;
use MozComics::MySQL;

use LWP::Simple;
use DateTime;
use DateTime::Format::Strptime;
use HTML::Entities qw(decode_entities);

# initialize comic object
my $guid = '40877285-11ae-4937-ba82-f2af9fe3a5e0';
#my $comic = MozComics::JSON->new($guid);
my $comic = MozComics::MySQL->new($guid,2);
my $comicName = $comic->getName();
my $urlPrefix = 'http://www.channelate.com';
my $escapedUrlPrefix = quotemeta($urlPrefix);
my $lastStripRow = $comic->getLastStrip();

# used to ensure that strips are put in the correct order
# (current 'strip' value should not be less than the previous 'strip' value)
my $lastStrip = 0;

my $dateParser = new DateTime::Format::Strptime(
	pattern => '%Y/%m/%d',
	locale  => 'en_US'
);

sub parseDateFromUrl {
	my $url = shift(@_) or die "$comicName: url not passed to parseDateFromUrl";
	$url =~ /^$escapedUrlPrefix\/(\d{4}\/\d{2}\/\d{2}\/).+\/$/
	        or die "$comicName: malformed url in parseDateFromUrl";
	my $dateString = $1;

	return $dateParser->parse_datetime($dateString)
	       or die "$comicName: could not parse date ($dateString)";
}

# scrape information from a page
sub scrapePage {
	my $url = shift(@_) or die "$comicName: url not passed to scrapePage";
	my $page = shift(@_) or die "$comicName: page not passed to scrapePage";
	my $data = {};
	$data->{url} = $url;

	# scrape 'strip' value
	my $strip = parseDateFromUrl($url)->epoch();

	if($strip <= $lastStrip) {
		die "$comicName: $url is out of order?";
	}
	$strip += 1000;
	$lastStrip = $strip;
	$data->{strip} = $strip;

	# scrape 'title', 'image' and 'extra' value
	my $regex = qr/\<div id="comic-1" class="comicpane"\>\<img src="$escapedUrlPrefix\/comics\/(.+?)" alt="(.+?)" title="(.+?)"\/\>\<\/div\>/;
	my @scraped = $comic->scrapeUnique($page, $regex);
	$data->{image} = "$urlPrefix/comics/$scraped[0]";
	my $title = decode_entities($scraped[1]);
	my $extra = decode_entities($scraped[2]);
	$data->{title} = $title;
	if($title ne $extra) {
		$data->{extra} = "{\"mouseover\":\"$extra\"}";
	}

	return $data;
}

# ensure the data of the last strip has not changed
my $stripPage = get($lastStripRow->{url})
                or die "$comicName: last page url not found";
my $lastStripData = scrapePage($lastStripRow->{url}, $stripPage);
$comic->assertStripsEqual($lastStripData, $lastStripRow);

# reset $lastStrip value since it was changed in the previous scrapePage call
$lastStrip = $lastStripRow->{strip};

# follow "Next" links in order to find new strips
while($stripPage =~ /\<a href="$escapedUrlPrefix\/(.+?)" class="navi navi-next" title=""\>\<\/a\>/) {
	my $url = "$urlPrefix/$1";
	$stripPage = get($url) or die "$comicName: next strip page not found";

	#print "$url\n";
	$comic->addStrip(scrapePage($url, $stripPage));
}

$comic->commit();

